#!/usr/bin/env python
# -*- coding: utf-8 -*-
import configparser
import os
import smtplib
from email.mime.text import MIMEText

def getDBConfig(section):
    config = configparser.ConfigParser()
    cron_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    print(cron_dir)
    path = cron_dir + '/config/config.ini'
    config.read(path)
    db_config = {}
    db_config["host"] = config.get(section, "host")
    db_config["user"] = config.get(section, "user")
    db_config["passwd"] = config.get(section, "passwd")
    db_config["port"] = config.getint(section, "port")
    db_config["db"] = config.get(section, "db")
    return db_config

def sendmail(mail,subject,receiver,messages):
    config = configparser.ConfigParser()
    cron_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    print(cron_dir)
    path = cron_dir + '/config/config.ini'
    config.read(path)

    sender = config.get(mail,'sender')
    receiver = receiver
    subject = str(subject)
    smtpserver = config.get(mail, 'smtpserver')
    username = config.get(mail,'user')
    password = config.get(mail, 'password')
    msg = MIMEText(messages,'html','utf-8')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ",".join(receiver)
    smtp = smtplib.SMTP(smtpserver)
    smtp.starttls()
    smtp.login(username, password)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()